package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sql.TestCasesDao;
import beans.TestItemBean;

/**
 * Servlet implementation class EditTestForUserStory
 */
@WebServlet("/EditTestForUserStory")
public class EditTestForUserStory extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditTestForUserStory() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		TestItemBean testItemBean = new TestItemBean();
		testItemBean.setName(request.getParameter("test-name"));
		testItemBean.setDescription(request.getParameter("test-description"));
		testItemBean.setExpectedResults(request.getParameter("test-expected-results"));
		try {
			String idString = request.getParameter("test-id");
			testItemBean.setId(Integer.parseInt(idString));
		}
		catch (NumberFormatException e) {
			e.printStackTrace();
		}

		TestCasesDao testCasesDao = new TestCasesDao();
		try {
			testCasesDao.update(testItemBean);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		
		// return data
		PrintWriter out = response.getWriter();
		out.write(testItemBean.getJson());
	}

}
